package com.kefu.web.im.util;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.kefu.web.im.config.IMDataContext;
import com.kefu.web.im.config.web.message.ChatMessage;
import com.kefu.web.im.domain.MessageOutContent;
import com.kefu.web.im.domain.SeatUser;
import com.kefu.web.im.domain.SeatUserTask;
import com.kefu.web.im.netty.client.NettyClients;

public class MessageUtils {
	/**
	 * 
	 * @param image
	 * @param userid
	 */
	public static void uploadImage(String image, String userid) {
		createMessage(image, IMDataContext.MediaTypeEnum.IMAGE.toString(), userid);
	}

	public static void uploadImage(String image, String userid, String username, String appid, String orgi) {
		createMessage(image, IMDataContext.MediaTypeEnum.IMAGE.toString(), userid, username, appid, orgi);
	}

	public static void createMessage(String message, String msgtype, String userid) {
		// TODO
//		SeatUser seatUser = (SeatUser) CacheHelper.getAgentUserCacheBean().getCacheObject(userid, IMDataContext.SYSTEM_ORGI);
		ChatMessage data = new ChatMessage();
		SeatUser seatUser = null;
		if (seatUser != null) {
			data.setUserid(seatUser.getUserid());
			data.setUsername(seatUser.getUsername());
			data.setTouser(seatUser.getSeatno());
			data.setAppid(seatUser.getAppid());
			data.setOrgi(seatUser.getOrgi());
			data.setMessage(message);
			data.setType(IMDataContext.MessageTypeEnum.MESSAGE.toString());
			createMessage(data, msgtype, userid);
		}
	}

	public static void createMessage(String message, String msgtype, String userid, String username, String appid,
			String orgi) {
		ChatMessage data = new ChatMessage();
		if (!StringUtils.isBlank(userid)) {
			data.setUserid(userid);
			data.setUsername(username);
			data.setTouser(userid);
			data.setAppid(appid);
			data.setOrgi(orgi);
			data.setMessage(message);
			data.setType(IMDataContext.MessageTypeEnum.MESSAGE.toString());
			createAiMessage(data, IMDataContext.CallTypeEnum.IN.toString(),
					IMDataContext.AiItemType.USERINPUT.toString(), IMDataContext.MediaTypeEnum.IMAGE.toString(),
					data.getUserid());
		}
	}

	public static void createMessage(ChatMessage data, String msgtype, String userid) {
//		AgentUser agentUser = (AgentUser) CacheHelper.getAgentUserCacheBean().getCacheObject(userid, IMDataContext.SYSTEM_ORGI);
		SeatUser seatUser = null;
		MessageOutContent outMessage = new MessageOutContent();
		outMessage.setMessage(data.getMessage());
		outMessage.setMessageType(msgtype);
		outMessage.setCalltype(IMDataContext.CallTypeEnum.IN.toString());
		outMessage.setSeatUser(seatUser);
		outMessage.setSnsAccount(null);

		MessageOutContent statusMessage = null;
		if (seatUser == null) {
			statusMessage = new MessageOutContent();
			statusMessage.setMessage(data.getMessage());
			statusMessage.setMessageType(IMDataContext.MessageTypeEnum.STATUS.toString());
			statusMessage.setCalltype(IMDataContext.CallTypeEnum.OUT.toString());
			statusMessage.setMessage("当前坐席全忙，请稍候");
		} else {
			data.setUserid(seatUser.getUserid());
			data.setUsername(seatUser.getUsername());
			data.setId(CommonTools.getUUID());
			data.setTouser(seatUser.getSeatno());

			data.setAgentuser(seatUser.getId());

			data.setAgentserviceid(seatUser.getSeatserviceid());

			data.setAppid(seatUser.getAppid());
			data.setOrgi(seatUser.getOrgi());

			data.setMsgtype(msgtype);

			data.setUsername(seatUser.getUsername());
			data.setUsession(seatUser.getUserid()); // agentUser作为 session id
			data.setContextid(seatUser.getContextid());
			data.setCalltype(IMDataContext.CallTypeEnum.IN.toString());
			if (!StringUtils.isBlank(seatUser.getSeatno())) {
				data.setTouser(seatUser.getSeatno());
			}
			data.setChannel(seatUser.getChannel());

			outMessage.setContextid(seatUser.getContextid());
			outMessage.setFromUser(data.getUserid());
			outMessage.setToUser(data.getTouser());
			outMessage.setChannelMessage(data);
			outMessage.setNickName(seatUser.getUsername());
			outMessage.setCreatetime(data.getCreatetime());

			/**
			 * 保存消息
			 */
			if (IMDataContext.MessageTypeEnum.MESSAGE.toString().equals(data.getType())) {
				// TODO
//    			IMDataContext.getContext().getBean(ChatMessageRepository.class).save(data) ;
			}
			if (data.getType() != null && data.getType().equals(IMDataContext.MessageTypeEnum.MESSAGE.toString())) {
				// TODO
//	    		AgentUserTaskRepository agentUserTaskRes = IMDataContext.getContext().getBean(AgentUserTaskRepository.class) ;
//    			SeatUserTask seatUserTask = SeatUserTaskRes.getOne(seatUser.getId()) ;
				SeatUserTask seatUserTask = null;
				seatUserTask.setLastmessage(new Date());
				seatUserTask.setWarnings("0");
				seatUserTask.setWarningtime(null);
				seatUserTask.setLastmsg(
						data.getMessage().length() > 100 ? data.getMessage().substring(0, 100) : data.getMessage());
				seatUserTask.setTokenum(seatUserTask.getTokenum() + 1);
				data.setTokenum(seatUserTask.getTokenum());
//	    		agentUserTaskRes.save(agentUserTask) ;
			}
		}
		if (!StringUtils.isBlank(data.getUserid())
				&& IMDataContext.MessageTypeEnum.MESSAGE.toString().equals(data.getType())) {
			NettyClients.getInstance().sendIMEventMessage(data.getUserid(),
					IMDataContext.MessageTypeEnum.MESSAGE.toString(), outMessage);
			if (statusMessage != null) {
				NettyClients.getInstance().sendIMEventMessage(data.getUserid(),
						IMDataContext.MessageTypeEnum.STATUS.toString(), statusMessage);
			}
		}
		if (seatUser != null && !StringUtils.isBlank(seatUser.getSeatno())) {
			// 将消息发送给 坐席
			NettyClients.getInstance().sendAgentEventMessage(seatUser.getSeatno(),
					IMDataContext.MessageTypeEnum.MESSAGE.toString(), data);
		}
	}

	public static ChatMessage createAiMessage(ChatMessage data, String direction, String chatype, String msgtype,
			String userid) {
		MessageOutContent outMessage = new MessageOutContent();
		outMessage.setMessage(data.getMessage());
		outMessage.setMessageType(msgtype);
		outMessage.setCalltype(direction);
		outMessage.setSeatUser(null);
		outMessage.setSnsAccount(null);

		{
			data.setUserid(userid);
			data.setUsername(data.getUsername());
			data.setId(CommonTools.getUUID());
			data.setTouser(IMDataContext.SYSTEM_ORGI);

			data.setAgentuser(userid);

			data.setAgentserviceid(data.getContextid());
			data.setChatype(chatype);

			data.setAppid(data.getAppid());
			data.setOrgi(data.getOrgi());

			data.setMsgtype(msgtype);

			data.setUsername(data.getUsername());
			data.setUsession(data.getUserid()); // agentUser作为 session id
			data.setContextid(data.getContextid());
			data.setCalltype(direction);

			outMessage.setContextid(data.getContextid());
			outMessage.setFromUser(data.getUserid());
			outMessage.setToUser(data.getTouser());
			outMessage.setChannelMessage(data);
			outMessage.setNickName(data.getUsername());
			outMessage.setCreatetime(data.getCreatetime());

			/**
			 * 保存消息
			 */
			if (IMDataContext.MessageTypeEnum.MESSAGE.toString().equals(data.getType())) {
				//TODO
//				IMDataContext.getContext().getBean(ChatMessageRepository.class).save(data);
			}
		}
		if (!StringUtils.isBlank(data.getUserid())
				&& IMDataContext.MessageTypeEnum.MESSAGE.toString().equals(data.getType())) {
			NettyClients.getInstance().sendIMEventMessage(data.getUserid(),
					IMDataContext.MessageTypeEnum.MESSAGE.toString(), outMessage);
		}
		return data;
	}

}
